package mfuny.ggongal.admin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import mfuny.ggongal.admin.advertisers.AdvertiserDao;
import mfuny.ggongal.admin.contents.ContentDao;
import mfuny.ggongal.admin.debug.Debug;
import mfuny.ggongal.admin.games.GameDao;
import mfuny.ggongal.admin.gift.GiftCardDao;
import mfuny.ggongal.admin.gift.GiftDao;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

@Controller
public class TestController {
	SqlMapClient sqlMapper;
	ModelAndView mav;
	
	public TestController() throws Exception {
		sqlMapper = Server.getSqlMapper();
		mav = new ModelAndView();
	}
	
	@RequestMapping("/test.ga")
	public ModelAndView testForm() throws Exception {
		mav.clear();
		mav.setViewName("/test/test.ga");
		return mav;
	}
	
	@RequestMapping("/sms.ga")
	public ModelAndView sms() throws Exception {
		mav.clear();
		mav.setViewName("/test/sms.ga");
		return mav;
	}
	
	//압축 테스트.
	@RequestMapping("/zipTest.ga")
	public ModelAndView zipTest() throws Exception {
		
		String msg = "실패";
		
		String[] files = {
			"1405581825227_9_0_TAL.jpg",
			"1405581825227_0_1_TAL.jpg",
			"1405581825227_71_2_TAL.jpg",
			"1405581825227_21_3_TAL.jpg",
			"1405581515021_9_0_balhae.jpg",
"1405406962118_33_0_balhae.jpg",
"1405406758896_14_0_balhae.jpg",
"1405320917246_32_0_coveri.jpg",
"1404463271727_78_0_irunani.jpg",
"1404368765260_60_0_TAL.jpg",
"gift_cgv_1_300px.jpg",
"1403766061702_14_0_balhae.jpg",
"1403751557992_77_0_digiworld.jpg",
"1403750095976_61_0_digiworld.jpg",
"1403749631735_41_0_jacklynpie.jpg",
"1403749399810_58_0_digiworld.jpg",
"1403749217072_47_0_jacklynpie.jpg",
"1403590685800_32_0_digiworld.jpg",
"1403590412878_52_0_jacklynpie.jpg",
"1403576328574_38_0_digiworld.jpg",
"gift_b500_cu_300px.jpg",
"1403507123651_83_0_garban.jpg",
"1403166120479_47_0_irunani.jpg",
"1403164933787_29_0_digiworld.jpg",
"1403164768832_14_0_irunani.jpg",
"1403164631583_12_0_jacklynpie.jpg",
"1403164145550_37_0_LiveLive.jpg",
"1403163897837_19_0_digiworld.jpg",
"1403163463923_14_0_jacklynpie.jpg",
"1402973781355_66_0_digiworld.jpg",
"1402973633795_7_0_irunani.jpg",
"1402973330406_6_0_jacklynpie.jpg",
"1402972104901_1_0_digiworld.jpg",
"1402972059599_16_0_LiveLive.jpg",
"1402971994952_63_0_jacklynpie.jpg",
"1402898314842_0_0_jacklynpie.jpg",
"1402898228605_11_0_digiworld.jpg",
"1402898174645_90_0_irunani.jpg",
"1402898081388_11_0_bong.jpg",
"1402816801026_96_0_digiworld.jpg",
"1402816729453_36_0_irunani.jpg",
"1402815456945_58_0_jacklynpie.jpg",
"1402562031769_29_0_irunani.jpg",
"1402560651937_82_0_digiworld.jpg",
"1402560508742_46_0_jacklynpie.jpg",
"1402559075231_4_0_digiworld.jpg",
"1402559007211_55_0_coveri.jpg",
"1402558932695_74_0_jacklynpie.jpg",
"1402558327444_71_0_digiworld.jpg",
"1402558174372_28_0_LiveLive.jpg",
"1402557880361_77_0_jacklynpie.jpg",
"1402397472254_59_0_digiworld.jpg",
"1402397411175_14_0_LiveLive.jpg",
"1402397299726_5_0_coveri.jpg",
"1402397133159_73_0_jacklynpie.jpg",
"1402393094644_23_0_digiworld.jpg",
"1402393011904_65_0_LiveLive.jpg",
"1402392891338_42_0_coveri.jpg",
"1402392272848_47_0_jacklynpie.jpg",
"1402307876666_17_0_digiworld.jpg",
"1402307785756_12_0_LiveLive.jpg",
"1402307723347_70_0_coveri.jpg",
"1402307629566_33_0_jacklynpie.jpg",
"1401883401651_57_0_digiworld.jpg",
"1401883319634_99_0_LiveLive.jpg",
"1401883205429_64_0_coveri.jpg",
"1401883000444_64_0_jacklynpie.jpg",
"1401882848518_54_0_digiworld.jpg",
"1401882714072_39_0_LiveLive.jpg",
"1401882611827_42_0_coveri.jpg",
"1401882518737_73_0_jacklynpie.jpg",
"1401882444757_24_0_digiworld.jpg",
"1401882345050_40_0_LiveLive.jpg",
"1401882270835_70_0_bong.jpg",
"1401882183165_23_0_jacklynpie.jpg",
"1401882085387_30_0_digiworld.jpg",
"1401882005291_53_0_LiveLive.jpg",
"1401881883414_21_0_coveri.jpg",
"1401881802137_81_0_jacklynpie.jpg",
"1401881361977_68_0_LiveLive.jpg",
"1401881229809_29_0_coveri.jpg",
"1401881125406_50_0_jacklynpie.jpg",
"1401790077652_91_0_digiworld.jpg",
"1401789418042_72_0_LiveLive.jpg",
"1401789295351_8_0_coveri.jpg"
		};
		
		byte[] buf = new byte[1024];
		
		try {
			String zipName = Server.filePath + "ziptest.zip";
			ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipName));
			
			//파일 압축.
			for(int i = 0; i < files.length; i++) {
				FileInputStream in = new FileInputStream(Server.filePath + files[i]);
				
				//압축 항목 추가.
				zipOut.putNextEntry(new ZipEntry(files[i]));
				
				//바이트 전송.
				int len;
				while((len = in.read(buf)) > 0) {
					zipOut.write(buf, 0, len);
				}
				
				zipOut.closeEntry();
				in.close();
			}
			
			//압축파일 작성.
			zipOut.close();
			
			msg = "성공";
			
		} catch ( Exception e ) {
			Debug.toDebugFile(e, "zipTest");
			msg = "실패";
		}
		
		mav.clear();
		mav.setViewName("blank.ga");
		mav.addObject("value", msg);
		return mav;
	}
	
	//이미지 찾아서 용량 자동으로 전부 때려넣기 폼..
	@RequestMapping("/imgCapacityForm.ga")
	public ModelAndView imgCapacityForm() throws Exception {
		mav.clear();
		mav.setViewName("/test/imgCapacityForm.ga");
		return mav;
	}
	
	//이미지 용량 처리.
	//게임 테이블.
	@SuppressWarnings("unchecked")
	@RequestMapping("/games_imgCapacity.ga")
	public ModelAndView games_imgCapacity() throws Exception {
		List<GameDao> gameList = (List<GameDao>) sqlMapper.queryForList("games_selectAll", null);
		int batchResult = -1;
		
		try {
			sqlMapper.startTransaction();
			sqlMapper.startBatch();
			
			for(int i = 0; i < gameList.size(); i++) {
				GameDao game = gameList.get(i);
				File imgFile = new File( Server.filePath + game.getImgmain_savname() );
				game.setImgmain_capacity( imgFile.exists() ? imgFile.length() : 0 );
				imgFile = new File( Server.filePath + game.getImgdetail1_savname() );
				game.setImgdetail1_capacity( imgFile.exists() ? imgFile.length() : 0 );
				imgFile = new File( Server.filePath + game.getImgdetail2_savname() );
				game.setImgdetail2_capacity( imgFile.exists() ? imgFile.length() : 0 );
				imgFile = new File( Server.filePath + game.getImgdetail3_savname() );
				game.setImgdetail3_capacity( imgFile.exists() ? imgFile.length() : 0 );
				
				sqlMapper.update("games_addCapacityAll", game);
			}
			
			
			batchResult = sqlMapper.executeBatch();
			sqlMapper.commitTransaction();
			
		} catch ( Exception e ) {
			Debug.toDebugFile(e, "games_imgCapacity");
			
		} finally {
			sqlMapper.endTransaction();
		}
		
		mav.clear();
		mav.addObject("value", batchResult);
		mav.setViewName("/blank");
		return mav;
	}
	
	//광고주 ci 이미지.
	@SuppressWarnings("unchecked")
	@RequestMapping("/adv_imgCapacity.ga")
	public ModelAndView adv_imgCapacity() throws Exception {
		List<AdvertiserDao> advList = (List<AdvertiserDao>) sqlMapper.queryForList("adv_selectAll", null);
		int batchResult = -1;
		
		try {
			sqlMapper.startTransaction();
			sqlMapper.startBatch();
			
			for(int i = 0; i < advList.size(); i++) {
				AdvertiserDao adv = advList.get(i);
				File imgFile = new File( Server.filePath + adv.getCi_img_savname() );
				adv.setCi_img_capacity( imgFile.exists() ? imgFile.length() : 0 );
				
				sqlMapper.update("adv_addCapacityAll", adv);
			}
			
			
			batchResult = sqlMapper.executeBatch();
			sqlMapper.commitTransaction();
			
		} catch ( Exception e ) {
			Debug.toDebugFile(e, "adv_imgCapacity");
			
		} finally {
			sqlMapper.endTransaction();
		}
		
		mav.clear();
		mav.addObject("value", batchResult);
		mav.setViewName("/blank");
		return mav;
	}

	// 컨텐츠 테이블.
	@SuppressWarnings("unchecked")
	@RequestMapping("/con_imgCapacity.ga")
	public ModelAndView con_imgCapacity() throws Exception {
		List<ContentDao> conList = (List<ContentDao>) sqlMapper.queryForList("con_selectAll", null);
		int batchResult = -1;

		try {
			sqlMapper.startTransaction();
			sqlMapper.startBatch();

			for (int i = 0; i < conList.size(); i++) {
				ContentDao con = conList.get(i);
				File imgFile = new File(Server.filePath + con.getImgcontents1_savname());
				con.setImgcontents1_capacity(imgFile.exists() ? imgFile.length() : 0);
				imgFile = new File(Server.filePath + con.getImgcontents2_savname());
				con.setImgcontents2_capacity(imgFile.exists() ? imgFile.length() : 0);
				imgFile = new File(Server.filePath + con.getImgcontents3_savname());
				con.setImgcontents3_capacity(imgFile.exists() ? imgFile.length() : 0);
				imgFile = new File(Server.filePath + con.getImgcontents4_savname());
				con.setImgcontents4_capacity(imgFile.exists() ? imgFile.length() : 0);

				sqlMapper.update("con_addCapacityAll", con);
			}

			batchResult = sqlMapper.executeBatch();
			sqlMapper.commitTransaction();

		} catch (Exception e) {
			Debug.toDebugFile(e, "con_imgCapacity");

		} finally {
			sqlMapper.endTransaction();
		}

		mav.clear();
		mav.addObject("value", batchResult);
		mav.setViewName("/blank");
		return mav;
	}
	
	//gift 이미지.
	@SuppressWarnings("unchecked")
	@RequestMapping("/gift_imgCapacity.ga")
	public ModelAndView gift_imgCapacity() throws Exception {
		List<GiftDao> giftList = (List<GiftDao>) sqlMapper.queryForList("gift_selectAll", null);
		int batchResult = -1;

		try {
			sqlMapper.startTransaction();
			sqlMapper.startBatch();

			for (int i = 0; i < giftList.size(); i++) {
				GiftDao gift = giftList.get(i);
				File imgFile = new File(Server.filePath + gift.getImg_savname());
				gift.setImg_capacity(imgFile.exists() ? imgFile.length() : 0);

				sqlMapper.update("gift_addCapacityAll", gift);
			}

			batchResult = sqlMapper.executeBatch();
			sqlMapper.commitTransaction();

		} catch (Exception e) {
			Debug.toDebugFile(e, "gift_imgCapacity");

		} finally {
			sqlMapper.endTransaction();
		}

		mav.clear();
		mav.addObject("value", batchResult);
		mav.setViewName("/blank");
		return mav;
	}
	
	//giftcard 이미지.
	@SuppressWarnings("unchecked")
	@RequestMapping("/card_imgCapacity.ga")
	public ModelAndView card_imgCapacity() throws Exception {
		List<GiftCardDao> cardList = (List<GiftCardDao>) sqlMapper.queryForList("card_selectAll", null);
		int batchResult = -1;

		try {
			sqlMapper.startTransaction();
			sqlMapper.startBatch();

			for (int i = 0; i < cardList.size(); i++) {
				GiftCardDao card = cardList.get(i);
				File imgFile = new File(Server.filePath + card.getCard_img_savname());
				card.setCard_img_capacity(imgFile.exists() ? imgFile.length() : 0);

				sqlMapper.update("card_addCapacityAll", card);
			}

			batchResult = sqlMapper.executeBatch();
			sqlMapper.commitTransaction();

		} catch (Exception e) {
			Debug.toDebugFile(e, "card_imgCapacity");

		} finally {
			sqlMapper.endTransaction();
		}

		mav.clear();
		mav.addObject("value", batchResult);
		mav.setViewName("/blank");
		return mav;
	}

}
